package com.gxa.service.zls.impl;

import com.gxa.common.R;
import com.gxa.mapper.zls.CustomerMapper;
import com.gxa.pojo.Customer;
import com.gxa.pojo.Customer_Contact_Peroson_Info;
import com.gxa.service.zls.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: 周林森
 * Date: 2021/12/06 09:00:03
 * Description: 客户相关业务
 * Version: V1.0
 */
@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired(required = false)
    private CustomerMapper customerMapper;

    /**
     * 添加新客户相关信息
     * @param map
     * @return
     */
    @Override
    public R addCustomer(Map map) {
        Map map1 = (Map)map.get("newcustomer");
        //查询新增客户id是否已经存在
        String customerId = customerMapper.queryCustomerIdExist(map1.get("customer_Id"));
        if(customerId != null) {
            return new R(500,"添加失败");
        }
        //添加客户基本信息
        customerMapper.addCustomerInfo(map1);
        //添加客户联系人信息
        List<Map<String,Object>> mapList = (List<Map<String, Object>>) map1.get("contact_peroson_infos");
        System.out.println(mapList);
//        if(mapList==null){
//            customerMapper.addContactNo(map1.get("customer_Id"));
//        }else{
        if(mapList !=null){
            for (Map<String,Object> map2:mapList){
                map2.put("customer_Id",map1.get("customer_Id"));
                customerMapper.addCustomerContactPeopleInfo(map2);
            }
        }
//        }
        return new R(200,"添加客户成功");
    }

    /**
     * 修改客户信息
     * @param map
     * @return
     */
    @Override
    public R updateCustomer(Map map) {
        Map newcustomer = (Map)map.get("newcustomer");
        //修改客户基本信息
        customerMapper.updateCustomerBaseInfo(newcustomer);
        //查询客户联系人记录编号
        List<Integer> list = customerMapper.queryCusContactId(newcustomer.get("customer_Id"));
        //修改客户联系人信息
        List<Map<String,Object>> mapList = (List<Map<String, Object>>) newcustomer.get("contact_peroson_infos");
        if(mapList.size() < list.size()){
            for (Map<String,Object> contactPerosonInfo: mapList) {
                Integer id = (Integer) contactPerosonInfo.get("contact_Person_Id");
                list.remove(id);
            }
            Integer[] array1 = new Integer[list.size()];
            for (int i =0; i < list.size(); i++){
                array1[i]=list.get(i);
            }
            customerMapper.delContactById(array1,newcustomer.get("customer_Id"));
        }else if(mapList.size() > list.size()){
            for (Map<String,Object> contactPerosonInfo: mapList) {
                Object id = contactPerosonInfo.get("contact_Person_Id");
                if(!(list.contains(id))){
                    contactPerosonInfo.put("Customer_Id",newcustomer.get("customer_Id"));
                    customerMapper.addCustomerContactPeopleInfo(contactPerosonInfo);
                }
            }
        }
        for (Map<String,Object> contactPerosonInfo: mapList) {
            customerMapper.updateCustomerContactInfo(contactPerosonInfo);
        }
        return new R(200,"修改客户信息成功");
    }

    /**
     * 查询客户信息
     */
    @Override
    public R queryCustomer(Map map) {
        List<Customer> customerList = customerMapper.queryCustomerList(map);
        return new R(200,"查询客户成功",customerList);
    }

    /**
     * 删除客户
     * @param  id
     * @return
     */
    @Override
    public R delCustomer(Object id) {
        int i = customerMapper.delCustomerById(id);
        return new R(200,"删除成功");
    }

    /**
     * 修改客户状态
     * @param customer_id
     * @param status
     * @return
     */
    @Override
    public R updateCustomerStatus(Object customer_id, Object status) {
        String param = "true";
        if("true".equals((String)status)){
            param = "false";
        }
        int i = customerMapper.updateCustomerStatus(param,customer_id);
        if (i>0){
            return new R(200,"修改状态成功");
        }
        return new R(200,"修改状态失败");
    }


}
